#-----------------------------------------------------
# Current Server Resources - CPU
#-----------------------------------------------------
#current server name
$servername = "ROGUE"

Get-WmiObject -Class Win32_ComputerSystem `
              -ComputerName $servername |
Select Name, 
       Domain, 
       NumberOfProcessors, 
       NumberOfLogicalProcessors |
Format-List


#-----------------------------------------------------
# Current Server Resources - CPU Usage
#-----------------------------------------------------
#current server name
$servername = "ROGUE"

Get-WmiObject -Class Win32_Processor -ComputerName $servername |
Measure-Object -Property LoadPercentage Average


#-----------------------------------------------------
# Current Server Resources - Memory
#-----------------------------------------------------
#current server name
$servername = "ROGUE"

Get-WmiObject -Class Win32_OperatingSystem `
              -ComputerName $servername |
Get-Member -MemberType Property |
Where-Object Name -Like "*Mem*" |
Select Name


#-----------------------------------------------------
# Current Server Resources - Memory
#-----------------------------------------------------
#current server name
$servername = "ROGUE"

Get-WmiObject -Class Win32_OperatingSystem -ComputerName $servername |
Select @{N="TotalVisibleMemorySize (GB)";E={"{0:N1}" -f (($_.TotalVisibleMemorySize)/1024/1024)}},
@{N="FreePhysicalMemory (GB)";E={"{0:N1}" -f (($_.FreePhysicalMemory)/1024/1024)}},
@{N="MemoryUsage %";E={ {0:N2} -f ((($_.TotalVisibleMemorySize - $_.FreePhysicalMemory)*100)/ $_.TotalVisibleMemorySize) }} |
Format-List


#-----------------------------------------------------
# Current Server Resources - Disk Space
#-----------------------------------------------------
#current server name
$servername = "ROGUE"

Get-WmiObject -Class Win32_LogicalDisk `
       -ComputerName $servername |
Select @{N="DeviceID";E={$_.DeviceID}},
       @{N="DriveType";
         E={switch ($_.DriveType)
                   {
                      0 {"Unknown"}
                      1 {"No Root Directory"}
                      2 {"Removable Disk"}
                      3 {"Local Disk"}
                      4 {"Network Drive"}
                      5 {"Compact Disc"}
                      6 {"RAM Disk"}
                   }};
         },
        @{N="Size (GB)";E={"{0:N1}" -f($_.Size/1GB)}},
        @{N="Free Space (GB)";E={"{0:N1}" -f($_.FreeSpace/1GB)}},
        @{N="Free Space (%)";
          E={
             if ($_.Size -gt 0)
             {
                "{0:P0}" -f($_.FreeSpace/$_.Size)
             }
             else
             {
                0
             }
            }
          } |
Format-Table -AutoSize


#-----------------------------------------------------
# Current Server Resources - Network
#-----------------------------------------------------
#current server name
$servername = "ROGUE"

Get-WmiObject -Class Win32_NetworkAdapterConfiguration `
        -ComputerName $servername `
        -Filter IPEnabled=True | 
Select Description, DHCPEnabled, 
       IPEnabled, IPAddress, 
       MACAddress


#-----------------------------------------------------
# Hotfixes and Service Packs
#-----------------------------------------------------
#current server name
$servername = "ROGUE"

Get-WmiObject -Class Win32_OperatingSystem `
              -ComputerName $servername |
Select CSName, Caption,
       ServicePackMajorVersion, 
       ServicePackMinorVersion |
Format-List


#-----------------------------------------------------
# Hotfixes and Service Packs
#-----------------------------------------------------
#current server name
$servername = "ROGUE"

Get-WmiObject -Class Win32_QuickFixEngineering `
              -ComputerName $servername |
Sort-Object -Property InstalledOn -Descending |
Format-Table AutoSize


#-----------------------------------------------------
# Current SQL Server Instances
#-----------------------------------------------------
Import-Module SQLPS -DisableNameChecking

#current server name
$servername = "ROGUE"

$managedComputer = New-Object "Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer" $servername

#list SQL Server instances
$managedComputer.ServerInstances |
Select Name, State, ServerProtocols, Urn |
Format-List



#-----------------------------------------------------
# Services and Service Accounts
#-----------------------------------------------------
Import-Module SQLPS -DisableNameChecking

#current server name
$servername = "ROGUE"

$managedComputer = New-Object "Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer" $servername

$managedComputer.Services |
Select Name, ServiceAccount, DisplayName |
Format-Table -AutoSize


#-----------------------------------------------------
# SQL Server Error Logs
#-----------------------------------------------------
Import-Module SQLPS -DisableNameChecking

#current server name
$servername = "ROGUE"

$server = New-Object "Microsoft.SqlServer.Management.Smo.Server" $servername

#display most recent 5 entries 
$server.ReadErrorLog() |
Select LogDate, ProcessInfo, Text, HasErrors  -Last 5  | 
Format-List


#-----------------------------------------------------
# Current Instance Configurations
#-----------------------------------------------------
#current server name
$servername = "ROGUE"

$server = New-Object "Microsoft.SqlServer.Management.Smo.Server" $servername

$server |
Get-Member |
Where Name -ne "SystemMessages" |
Where MemberType -eq "Property" |
Select Name,
       @{N="Value";E={$server.($_.Name)}} |
Format-Table -AutoSize


#-----------------------------------------------------
# Changing Configurations - Start or Stop a Service
#-----------------------------------------------------
Get-Command -Name "*Service*" -CommandType "Cmdlet"
$servicename = "SQLAgent`$SQL2014"
Stop-Service -Name $servicename
Start-Service -Name $servicename


#-----------------------------------------------------
# Changing Configurations - Change Service Account
#-----------------------------------------------------
Import-Module SQLPS -DisableNameChecking

#current server name
$servername = "ROGUE"

$managedComputer = New-Object "Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer" $servername

$servicename = "SQLAgent`$SQL2014"

$sqlservice = $managedComputer.Services |
              Where Name -EQ $servicename

#check current service account 
$sqlservice.ServiceAccount

#set new service account
$newserviceaccount = "QUERYWORKS\sqlagentservice"
#$newserviceaccount = "QUERYWORKS\sqlservice"
$credential = Get-Credential -Credential $newserviceaccount
$sqlservice.SetServiceAccount($credential.UserName,$credential.GetNetworkCredential().Password)

#check new service account 
$sqlservice.ServiceAccount


#-----------------------------------------------------
# Changing Configurations - Change Instance Settings
#-----------------------------------------------------
Import-Module SQLPS -DisableNameChecking

#current server name
$servername = "ROGUE"

$server = New-Object "Microsoft.SqlServer.Management.Smo.Server" $servername

#check current backup directory 
$server.BackupDirectory

#change backup directory 
$dir = "C:\Temp"
$server.BackupDirectory = $dir 
$server.Alter()

#check current backup directory 
$server.BackupDirectory

#enable xp_cmdshell
$server.Configuration.XPCmdShellEnabled.ConfigValue = 1
$server.Configuration.Alter()
$server.Configuration.XPCmdShellEnabled

#change AuditLevel
$server.Settings.AuditLevel = [Microsoft.SqlServer.Management.Smo.AuditLevel]::All

#make changes permanent
$server.Settings.Alter()

#display new settings
$server.Settings
